#!/usr/bin/env python
# yongming.li

import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import os
import os.path
from Androidcontrol import *



class SimulateTouch(QtGui.QMainWindow):
    
    androidcontrol = Androidcontrol()
    
    #public static final int KEYCODE_HOME            = 3;
    #public static final int KEYCODE_BACK            = 4;
    #public static final int KEYCODE_RECENTAPP       = 220;  
    # more details you can refer : KeyEvent.java

    def takescreenshot(self):
        cmd = 'adb shell /system/bin/screencap   /data/xmeter.png'
        os.popen(cmd)
        cmd = 'adb pull  /data/xmeter.png '+self.myPath+ '/xmeter.png'
        os.popen(cmd)

    def powerkey(self):
        print  'powerkey'
        cmd = 'adb shell input keyevent 26'
        os.popen(cmd)
    def backkey(self):
        cmd = 'adb shell input keyevent 4'
        os.popen(cmd)
    def homekey(self):
        print  'powerkey'
        cmd = 'adb shell input keyevent 3'
        os.popen(cmd)
    def recentkey(self):
        print  'powerkey'
        cmd = 'adb shell input keyevent 220'
        os.popen(cmd)
        
    def createkeyControl(self):
        self.Controlkey = QtGui.QGroupBox("key")
        layout = QtGui.QVBoxLayout()

        self.powerkeyButton = QtGui.QPushButton("power key")
        self.backButton = QtGui.QPushButton("back key")
        self.homekeyButton = QtGui.QPushButton("home key")
        self.recentkeyButton = QtGui.QPushButton("recent key")

  
        self.connect(self.powerkeyButton, SIGNAL('clicked()'), self.powerkey)
        self.connect(self.backButton, SIGNAL('clicked()'), self.backkey)
        self.connect(self.homekeyButton, SIGNAL('clicked()'), self.homekey)
        self.connect(self.recentkeyButton, SIGNAL('clicked()'), self.recentkey)
        
        layout.addWidget(self.powerkeyButton)
        layout.addWidget(self.backButton)

        layout.addWidget(self.homekeyButton)
        layout.addWidget(self.recentkeyButton)

        self.Controlkey.setLayout(layout)

    def createBrightnessControl(self):
        self.ControlBrightness = QtGui.QGroupBox("control Brightness  ")
        layout = QtGui.QHBoxLayout()

        dialBrightness = QDial()
        dialBrightness.setRange(0,255)
        dialBrightness.setNotchesVisible(True)
        #dial.setWrapping (True)
        spinboxBrightness = QSpinBox()
        spinboxBrightness.setRange(0,255)
        layout.addWidget(dialBrightness)
        layout.addWidget(spinboxBrightness)

        

        self.ControlBrightness.setLayout(layout)

        self.connect(dialBrightness, SIGNAL("valueChanged(int)"),SimulateTouch.androidcontrol.setbrightness)
        self.connect(dialBrightness, SIGNAL("valueChanged(int)"),spinboxBrightness.setValue)
        self.connect(spinboxBrightness, SIGNAL("valueChanged(int)"),dialBrightness.setValue)
        self.connect(spinboxBrightness, SIGNAL("valueChanged(int)"),SimulateTouch.androidcontrol.setbrightness)

    def  timeout(self):
        self.takescreenshot()
        #self.screencapLabel.setPixmap(QPixmap.fromImage(QImage('../screencap/xmeter.png')).scaled(160,250))
        #path = os.getcwd()
        #path = os.path.abspath(__file__)
        #print path
        self.screencapLabel.setPixmap(QPixmap.fromImage(QImage(self.myPath+'/xmeter.png')))
    def mousePressEvent (self,ev):
        print 'press'
        self.old_x=ev.x()
        self.old_y=ev.y()
        cmd = 'adb shell input  tap %d %d'%(ev.x() , ev.y())
        os.popen(cmd)

    def mouseReleaseEvent (self,ev):
        print  'release'
        cmd = 'adb shell input tap %d %d'%(ev.x() , ev.y())
        os.popen(cmd)
    def mouseMoveEvent (self, ev):
        print 'move'

        cmd = 'adb shell input swipe %d %d %d %d'%(self.old_x, self.old_y ,ev.x() , ev.y())
        print cmd
        self.old_x=ev.x()
        self.old_y=ev.y()
        os.popen(cmd)

    def __init__(self, parent=None):
        super(SimulateTouch, self).__init__(parent)
        self.myPath = os.path.dirname(__file__)
        print self.myPath

        self.createBrightnessControl()
        self.createkeyControl()
        vlayout = QtGui.QVBoxLayout()
        vlayout.addWidget(self.ControlBrightness)
        vlayout.addWidget(self.Controlkey)

        self.centralWidget = QtGui.QWidget()
        self.setCentralWidget(self.centralWidget)

        self.screencapLabel = QtGui.QLabel('')
        self.connect(self.screencapLabel, SIGNAL('mousePressEvent()'), self.mousePressEvent)

        mainLayout = QGridLayout()
        mainLayout.addLayout(vlayout,0,1)
        mainLayout.addWidget(self.screencapLabel,0,0)

        self.centralWidget.setLayout(mainLayout)

        self.setWindowTitle("SimulateTouch")
        self.timer = QTimer()
        self.connect(self.timer, SIGNAL('timeout()'), self.timeout)
        self.timer.start(1000);
        self.timeout()

        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = None
    form = SimulateTouch()
    sys.exit(app.exec_())




